package com.talkweb.historylib.provider;

import com.talkweb.historylib.ResourceTable;
import com.talkweb.securitycamera.common.interfaces.entity.IBaseFileEntity;
import com.talkweb.securitycamera.utils.photocodeclib.PhotoDecode;
import ohos.agp.components.*;
import ohos.agp.utils.TextTool;
import ohos.media.image.PixelMap;

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;


/**
 * @version 1.0.1
 * @description:
 * @program: security-camera
 * @Author xiaozhijun
 * @Date 2021/2/4 9:24
 */
public class ImageShowProvider extends PageSliderProvider {

    private final List<IBaseFileEntity> mImageData;

    //页面component组件缓存，主要保存当前处于活跃状态的component组件
    private final HashMap<Integer, Component> mPageSliderComponentCache;
    //页面component组件回收站
    private final Queue<Component> mPageSliderComponentRecyclers;

    public ImageShowProvider() {
        mImageData = new ArrayList<>();
        mPageSliderComponentCache = new HashMap<>();
        mPageSliderComponentRecyclers = new LinkedBlockingQueue<>();
    }

    public void setImageData(List<IBaseFileEntity> imageData) {
        this.mImageData.clear();
        if (imageData != null && !imageData.isEmpty()) {
            mImageData.addAll(imageData);
        }
    }

    @Override
    public int getCount() {
        return mImageData.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        if (componentContainer == null || position >= getCount()) {
            return Optional.empty();
        }

        //从视图回收站中获取组件，如果没有创建onBindViewHolder enter新的组件实例
        Component component = mPageSliderComponentRecyclers.poll();
        if (component == null) {
            ViewHolder viewHolder = ViewHolder.createViewHolder(componentContainer.getContext(), componentContainer, ResourceTable.Layout_item_image_slide);
            component = viewHolder.getConvertComponent();
        }

        updatePageComponentContent(component, mImageData.get(position));

        mPageSliderComponentCache.put(position, component);
        componentContainer.addComponent(component);
        return component;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object object) {
        if (componentContainer == null || position >= getCount()) {
            return;
        }

        if (object instanceof Component) {
            componentContainer.removeComponent((Component) object);
            mPageSliderComponentCache.remove(position);
            //回收已经被销毁的视图组件
            mPageSliderComponentRecyclers.offer((Component) object);
        }
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return component == object;
    }

    protected void updatePageComponentContent(Component component, IBaseFileEntity data) {
        if (data == null) {
            return;
        }
        Image image = (Image) component.findComponentById(ResourceTable.Id_iv_image);
        PixelMap pixelMap = new PhotoDecode.Builder().build().commonDecodePhoto(data.getFilePath());
        image.setPixelMap(pixelMap);
    }

    public IBaseFileEntity getItem(int position) {
        if (position >= 0 && position < mImageData.size()) {
            return mImageData.get(position);
        }
        return null;
    }

    /**
     * 删除数据并更新UI，这里更新后会回到第一张的位置，需在外面调用setCurrentPage回到删除的位置
     */
    public final void deleteItem(int position) {
        if (position >= 0 && position < mImageData.size()) {
            mImageData.remove(position);
            mPageSliderComponentCache.remove(position);
            notifyDataChanged();
        }
    }


}
